/*
 * Copyright 2013 meichenhui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.zet.tool.pluginanalyzer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Manifest;

import org.apache.commons.io.FileUtils;

/**
 * 不同形式输入路径的解析策略
 * 
 * eclipse 路径下有plugins和dropins两个目录 深度4/6级 
 * dropins 深度6级 
 * plugins 深度4级
 * plugin.jar 
 * plugin 路径下有META-INF目录 深度3级
 * 
 * @author meichenhui
 * 
 */
public class InputAnalyzePolicy {
    
    /**
     * eclipse形式插件 路径下有plugins或dropins目录 深度7级
     * 
     * @param file
     * @return
     */
    public boolean isEclipseDirectory(File file) {
        return false;
    }

    public List<SimpleBundleDescription> eclipseDirectoryPolicy(File file) throws FileNotFoundException, ManifestNotFoundException, IOException {
        File[] plugins = file.listFiles();
        List<SimpleBundleDescription> bundles = new ArrayList<SimpleBundleDescription>();
        for (File aPlugin : plugins) {
            if (isPluginsDirectory(aPlugin)) {
                List<SimpleBundleDescription> aBundleList = pluginsDirectoryPolicy(aPlugin);
                if (aBundleList != null) {
                    bundles.addAll(aBundleList);
                }
                continue;
            }
            if (isDropinsDirectory(aPlugin)) {
                List<SimpleBundleDescription> aBundleList = dropinsDirectoryPolicy(aPlugin);
                if (aBundleList != null) {
                    bundles.addAll(aBundleList);
                }
                continue;
            }
        }
        return bundles;
    }

    private boolean isPluginsDirectory(File file) {
        return false;
    }

    /**
     * plugins 深度4级
     * 
     * @param file
     * @return
     * @throws IOException 
     * @throws ManifestNotFoundException 
     * @throws FileNotFoundException 
     */
    private List<SimpleBundleDescription> pluginsDirectoryPolicy(File file) throws FileNotFoundException, ManifestNotFoundException, IOException {
        File[] plugins = file.listFiles();
        List<SimpleBundleDescription> bundles = new ArrayList<SimpleBundleDescription>();
        for (File aPlugin : plugins) {
            if (isPluginDirectory(aPlugin)) {
                SimpleBundleDescription aBundle = pluginDirectoryPolicy(aPlugin);
                if (aBundle != null) {
                    bundles.add(aBundle);
                }
                continue;
            }
            if (isPluginJar(aPlugin)) {
                SimpleBundleDescription aBundle = pluginJarPolicy(aPlugin);
                if (aBundle != null) {
                    bundles.add(aBundle);
                }
                continue;
            }
        }
        return bundles;
    }

    /**
     * dropins/plugins形式插件 深度6级
     * 
     * @param file
     * @return
     */
    private boolean isDropinsDirectory(File file) {
        return false;
    }

    private List<SimpleBundleDescription> dropinsDirectoryPolicy(File file) throws FileNotFoundException, ManifestNotFoundException, IOException {
        File[] plugins = file.listFiles();
        List<SimpleBundleDescription> bundles = new ArrayList<SimpleBundleDescription>();
        for (File aPlugin : plugins) {
            if (isPluginsDirectory(aPlugin)) {
                List<SimpleBundleDescription> aBundleList = pluginsDirectoryPolicy(aPlugin);
                bundles.addAll(aBundleList);
                continue;
            }
            if (isPluginDirectory(aPlugin)) {
                SimpleBundleDescription aBundle = pluginDirectoryPolicy(aPlugin);
                if (aBundle != null) {
                    bundles.add(aBundle);
                }
                continue;
            }
            if (isPluginJar(aPlugin)) {
                SimpleBundleDescription aBundle = pluginJarPolicy(aPlugin);
                if (aBundle != null) {
                    bundles.add(aBundle);
                }
                continue;
            }
        }
        return bundles;
    }

    private boolean isPluginJar(File file) {
        return false;
    }

    private SimpleBundleDescription pluginJarPolicy(File file) throws ManifestNotFoundException {
        return Util.createBundleDescription(file.getPath());
    }

    private boolean isPluginDirectory(File file) {
        return false;
    }

    private SimpleBundleDescription pluginDirectoryPolicy(File file) throws FileNotFoundException, ManifestNotFoundException, IOException {
        File manifest = FileUtils.getFile(file.getAbsolutePath() + "/META-INF/MANIFEST.MF");
        return Util.createBundleDescription(new Manifest(new FileInputStream(manifest)));
    }
    

}
